//
//  KYVideoCheckBox.m
//  KYVideoModule
//
//  Created by leoking870 on 2017/9/30.
//

#import "KYVideoCheckBox.h"

@interface KYVideoCheckBox ()<CAAnimationDelegate>
/** The layer where the box is drawn when the check box is set to On.
 */
@property (strong, nonatomic) CAShapeLayer *onBoxLayer;

/** The layer where the box is drawn when the check box is set to Off.
 */
@property (strong, nonatomic) CAShapeLayer *offBoxLayer;

/** The layer where the check mark is drawn when the check box is set to On.
 */
@property (strong, nonatomic) CAShapeLayer *checkMarkLayer;
@end

IB_DESIGNABLE
@implementation KYVideoCheckBox

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) [self commonInit];
    return self;
}

- (void)commonInit {
    // Default values
    _on = NO;
    
    _onTintColor = [UIColor colorWithRed:0 green:122.0/255.0 blue:255/255 alpha:1];
    _tintColor = [UIColor lightGrayColor];
    _lineWidth = 1.0;
    _cornerRadius = 2.0;
    
    _minimumTouchSize = CGSizeMake(44, 44);
    self.backgroundColor = [UIColor clearColor];
    
    
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapCheckBox:)]];
}
- (void)handleTapCheckBox:(UITapGestureRecognizer *)recognizer {
    // If we have a group that requires a selection, and we're already selected, don't allow a deselection
    self.on = !self.on;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}
#pragma mark Increase touch area
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event;
{
    BOOL found = [super pointInside:point withEvent:event];
    
    CGSize minimumSize = self.minimumTouchSize;
    CGFloat width = CGRectGetWidth(self.bounds);
    CGFloat height = CGRectGetHeight(self.bounds);
    
    if (found == NO && (width < minimumSize.width || height < minimumSize.height)) {
        CGFloat increaseWidth = minimumSize.width - width;
        CGFloat increaseHeight = minimumSize.height - height;
        
        CGRect rect = CGRectInset(self.bounds, (-increaseWidth / 2), (-increaseHeight / 2));
        
        found = CGRectContainsPoint(rect, point);
    }
    
    return found;
}
#pragma mark Drawings
- (void)drawRect:(CGRect)rect {
//    self.on = !self.on;
    [self setOn:_on];
}
- (void)setOn:(BOOL)on {
    _on = on;
    [self drawEntireCheckBox];
    
    if (on) {
//        [self addOnAnimation];
    } else {
        [self.onBoxLayer removeFromSuperlayer];
        [self.checkMarkLayer removeFromSuperlayer];
    }
    
}

- (void)drawEntireCheckBox {
    if (!self.offBoxLayer) {
        [self drawOffBox];
    }
    if (self.on) {
        [self drawOnBox];
    }
    if (self.on) {
        [self drawCheckMark];
    }
}

- (void)drawOffBox {
    [self.offBoxLayer removeFromSuperlayer];
    self.offBoxLayer = [CAShapeLayer layer];
    self.offBoxLayer.frame = self.bounds;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0 + (self.lineWidth / 2.0), 0.0 + (self.lineWidth / 2.0), CGRectGetHeight(self.frame) - self.lineWidth, CGRectGetHeight(self.frame) - self.lineWidth) cornerRadius:self.cornerRadius];
    [path applyTransform:CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * 2.5)];
    [path applyTransform:CGAffineTransformMakeTranslation(CGRectGetHeight(self.frame), 0)];
    
    self.offBoxLayer.path = path.CGPath;
    self.offBoxLayer.fillColor = [UIColor clearColor].CGColor;
    self.offBoxLayer.strokeColor = self.tintColor.CGColor;
    self.offBoxLayer.lineWidth = self.lineWidth;
    self.offBoxLayer.rasterizationScale = 2.0 * [UIScreen mainScreen].scale;
    self.offBoxLayer.shouldRasterize = YES;
    [self.layer addSublayer:self.offBoxLayer];
}

- (void)drawOnBox {
    [self.onBoxLayer removeFromSuperlayer];
    self.onBoxLayer = [CAShapeLayer layer];
    self.onBoxLayer.frame = self.bounds;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0 + (self.lineWidth / 2.0), 0.0 + (self.lineWidth / 2.0), CGRectGetHeight(self.frame) - self.lineWidth, CGRectGetHeight(self.frame) - self.lineWidth) cornerRadius:self.cornerRadius];
    [path applyTransform:CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * 2.5)];
    [path applyTransform:CGAffineTransformMakeTranslation(CGRectGetHeight(self.frame), 0)];
    self.onBoxLayer.path = path.CGPath;
    self.onBoxLayer.lineWidth = self.lineWidth;
    self.onBoxLayer.fillColor = [UIColor clearColor].CGColor;
    self.onBoxLayer.strokeColor = self.onTintColor.CGColor;
    self.onBoxLayer.rasterizationScale = 2.0 * [UIScreen mainScreen].scale;
    self.onBoxLayer.shouldRasterize = YES;
    [self.layer addSublayer:self.onBoxLayer];
}

- (void)drawCheckMark{
    [self.checkMarkLayer removeFromSuperlayer];
    self.checkMarkLayer = [CAShapeLayer layer];
    self.checkMarkLayer.frame = self.bounds;
    
    UIBezierPath* checkMarkPath = [UIBezierPath bezierPath];
    
    [checkMarkPath moveToPoint: CGPointMake(CGRectGetHeight(self.frame)/3.1578, CGRectGetHeight(self.frame)/2)];
    [checkMarkPath addLineToPoint: CGPointMake(CGRectGetHeight(self.frame)/2.0618, CGRectGetHeight(self.frame)/1.57894)];
    [checkMarkPath addLineToPoint: CGPointMake(CGRectGetHeight(self.frame)/1.3953, CGRectGetHeight(self.frame)/2.7272)];
    
    
    [checkMarkPath applyTransform:CGAffineTransformMakeScale(1.5, 1.5)];
    [checkMarkPath applyTransform:CGAffineTransformMakeTranslation(-CGRectGetHeight(self.frame)/4, -CGRectGetHeight(self.frame)/4)];
    
    self.checkMarkLayer.path = checkMarkPath.CGPath;
    self.checkMarkLayer.strokeColor = self.onTintColor.CGColor;
    self.checkMarkLayer.lineWidth = self.lineWidth;
    self.checkMarkLayer.fillColor = [UIColor clearColor].CGColor;
    self.checkMarkLayer.lineCap = kCALineCapRound;
    self.checkMarkLayer.lineJoin = kCALineJoinRound;
    
    self.checkMarkLayer.rasterizationScale = 2.0 * [UIScreen mainScreen].scale;
    self.checkMarkLayer.shouldRasterize = YES;
    [self.layer addSublayer:self.checkMarkLayer];
}

- (CABasicAnimation *)strokeAnimationReverse:(BOOL)reverse {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    if (reverse) {
        animation.fromValue = [NSNumber numberWithFloat:1.0];
        animation.toValue = [NSNumber numberWithFloat:0.0];
    } else {
        animation.fromValue = [NSNumber numberWithFloat:0.0];
        animation.toValue = [NSNumber numberWithFloat:1.0];
    }
    animation.duration = 5;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    return animation;
}

//- (void)addOnAnimation {
//    CABasicAnimation *animation = [self strokeAnimationReverse:NO];
//
//    [self.onBoxLayer addAnimation:animation forKey:@"strokeEnd"];
//    animation.delegate = self;
//    [self.checkMarkLayer addAnimation:animation forKey:@"strokeEnd"];
//}
//#pragma mark Animation Delegate
//- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//    if (flag == YES) {
//        if (self.on == NO) {
//            [self.onBoxLayer removeFromSuperlayer];
//            [self.checkMarkLayer removeFromSuperlayer];
//        }
//
//
//    }
//}
@end
